package com.edu.app.controller.ai;

import com.edu.app.constant.SysMessage;
import com.edu.app.model.customer.PendingActivityModel;
import com.edu.app.model.customer.RenewContractModel;
import com.edu.app.service.sys.ai.SysAITeachersService;
import com.edu.app.util.ErrorUtil;
import com.edu.app.util.MsgUtil;

import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RequestMapping(value = "/api/ai/teachers")
@RestController
public class SysAITeachersController {
    @Autowired
    SysAITeachersService sysAITeachersService;

    private String getOrder(String str){

        if(str.equalsIgnoreCase("name")){
            return "CAST(name AS CHAR CHARACTER SET gbk)";
        }else if(str.equalsIgnoreCase("class_name")){
            return "class_name";
        }else if(str.equalsIgnoreCase("class_points")){
            return "class_points";
        }else if(str.equalsIgnoreCase("time")){
            return "time";
        }else{
            return "CAST(name AS CHAR CHARACTER SET gbk)";
        }
    }

    private String getPoOrder(String str){

        if(str.equalsIgnoreCase("name")){
            return "CAST(name AS CHAR CHARACTER SET gbk)";
        }else if(str.equalsIgnoreCase("possibility")){
            return "possibility";
        }else if(str.equalsIgnoreCase("stage")){
            return "stage";
        }else if(str.equalsIgnoreCase("consultant")){
            return "CAST(consultant AS CHAR CHARACTER SET gbk)";
        }else if(str.equalsIgnoreCase("time")){
            return "time";
        }else if(str.equalsIgnoreCase("comments")){
            return "comments";
        }else{
            return "CAST(name AS CHAR CHARACTER SET gbk)";
        }
    }

    private String getSeq(String str){
        if(str.equalsIgnoreCase("asc")){
            return "asc";
        }else if(str.equalsIgnoreCase("desc")){
            return "desc";
        }else{
            return "asc";
        }
    }

    @GetMapping("pages/getMyStudentsModel")
    @RequiresAuthentication
    public ModelMap getMyStudentsModel(@RequestParam Integer offset,
                                       @RequestParam Integer limit,
                                       @RequestParam String orderBy,
                                       @RequestParam String sequence,
                                       @RequestParam String username,
                                       @RequestParam Integer status){
        try{
            String order = getOrder(orderBy);
            String seq = getSeq(sequence);

            PageInfo pageInfo = sysAITeachersService.getMyStudentsModel(offset, limit, order, seq, username, status);
            return MsgUtil.retMsg(SysMessage.GET_STUDENT_LIST_SUCCEED, "page_info", pageInfo);
        }catch (Exception e){
            return ErrorUtil.retErrMsg(e);
        }
    }

    @GetMapping("pages/getMyPoStudentsModel")
    @RequiresAuthentication
    public ModelMap getMyPoStudentsModel(@RequestParam Integer offset,
                                         @RequestParam Integer limit,
                                         @RequestParam String orderBy,
                                         @RequestParam String sequence,
                                         @RequestParam String username,
                                         @RequestParam Integer status){
        try{
            String order = getPoOrder(orderBy);
            String seq = getSeq(sequence);

            PageInfo pageInfo = sysAITeachersService.getMyPoStudentsModel(offset, limit, order, seq, username, status);
            return MsgUtil.retMsg(SysMessage.GET_STUDENT_LIST_SUCCEED, "page_info", pageInfo);
        }catch (Exception e){
            return ErrorUtil.retErrMsg(e);
        }
    }

    @GetMapping("/getRenewContractModelListByUser")
    @RequiresAuthentication
    public ModelMap getRenewContractModelListByUser(@RequestParam String username){
        try{
            List<RenewContractModel> renewContractModelList = sysAITeachersService.getRenewContractModelListByUser(username);
            return MsgUtil.retMsg(SysMessage.GET_RENEW_CONTRACT_LIST_SUCCEED, "renew_contract_list", renewContractModelList);
        }catch (Exception e){
            return ErrorUtil.retErrMsg(e);
        }
    }

    @GetMapping("/getRenewContractModelListAllByUser")
    @RequiresAuthentication
    public ModelMap getRenewContractModelListAllByUser(@RequestParam String username){
        try{
            List<RenewContractModel> renewContractModelList = sysAITeachersService.getRenewContractModelListAllByUser(username);
            return MsgUtil.retMsg(SysMessage.GET_RENEW_CONTRACT_LIST_SUCCEED, "renew_contract_list", renewContractModelList);
        }catch (Exception e){
            return ErrorUtil.retErrMsg(e);
        }
    }

    @GetMapping("/getRenewContractModelList")
    @RequiresAuthentication
    public ModelMap getRenewContractModelList(){
        try{
            List<RenewContractModel> renewContractModelList = sysAITeachersService.getRenewContractModelList();
            return MsgUtil.retMsg(SysMessage.GET_RENEW_CONTRACT_LIST_SUCCEED, "renew_contract_list", renewContractModelList);
        }catch (Exception e){
            return ErrorUtil.retErrMsg(e);
        }
    }

    @GetMapping("pages/getRenewContractModelListAll")
    @RequiresAuthentication
    public ModelMap getRenewContractModelListAll(@RequestParam Integer offset,
                                                 @RequestParam Integer limit){
        try{
            PageInfo pageInfo = sysAITeachersService.getRenewContractModelListAll(offset, limit);
            return MsgUtil.retMsg(SysMessage.GET_RENEW_CONTRACT_LIST_SUCCEED, "page_info", pageInfo);
        }catch (Exception e){
            return ErrorUtil.retErrMsg(e);
        }
    }

    @GetMapping("/getPendingActivityModelList")
    @RequiresAuthentication
    public ModelMap getPendingActivityModelList(){
        try{
            List<PendingActivityModel> pendingActivityModelList = sysAITeachersService.getPendingActivityModelList();
            return MsgUtil.retMsg(SysMessage.GET_PENDING_ACTIVITY_MODEL_LIST_SUCCEED, "pending_activity_list", pendingActivityModelList);
        }catch (Exception e){
            return ErrorUtil.retErrMsg(e);
        }
    }

}
